﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TPOJ.BLL;
using TPOJ.Model;
using System.Xml.Linq;

namespace TPOJ.Web
{
    public class Utils
    {
        public static string UserFriendlyResult(byte result)
        {
            string[] strs = { "Pending", "Compiling", "Running", "Judging", "Time Limit Exceeded", "Memory Limit Exceeded", "Output Limit Exceeded", "Representation Error", "Restricted Usage", "Wrong Answer", "Compile Error", "Runtime Error", "System Error", "Accepted" };
            if (result >= 0 && result < strs.Length)
            {
                return strs[result];
            }
            return "System Error";
        }

        public static string ResultCSS(byte result)
        {
            string[] strs = { "pending", "compiling", "running", "judging", "TLE", "MLE", "OLE", "PE", "RU", "WA", "CE", "RE", "SE", "AC" };
            if (result >= 0 && result < strs.Length)
            {
                return strs[result];
            }
            return "SE";
        }

        public static string GetUserName(int userId)
        {
            AccountBO accountBO = new AccountBO();
            Account account = accountBO.GetAccount(userId);
            return account.Name;
        }

        public static int ComputeRatio(int ac, int summit)
        {
            if (summit == 0)
                return 0;
            return ac * 100 / summit;
        }

        public static string ComputeStatus(DateTime startDate, DateTime endDate)
        {
            if (DateTime.Now.CompareTo(endDate) >= 0)
                return "Ended";
            if (DateTime.Now.CompareTo(startDate) >= 0)
                return "Running";
            return "Scheduled";
        }

        public static string FormatStanding(int? value)
        {
            if (value == null || value.Value == 0)
                return "";
            if (value == -1 || value == -2)
                return "--";
            return value.ToString();
        }

        public static string StandingCSSClass(int? value)
        {
            if (value == null || value.Value == 0)
                return "";
            if (value == -1)
                return "pending";
            if (value == -2)
                return "attempted";
            return "solved";
        }

        public static string FormatStandingCell(int? time, int? submits, int? wa_submits)
        {
            string s = FormatStanding(submits);
            if (StandingCSSClass(time).Equals("pending"))
            {
                s = wa_submits.ToString() + "+" + (submits - wa_submits);
            }

            return s + "<br />" + FormatStanding(time);
        }
    }
}